/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.AsistanceControl;
import com.click.app.model.Employee;
import com.click.app.model.LabelValue;
import com.click.app.model.PaymentsSalary;
import com.click.app.model.Salary;
import com.click.app.service.AsistanceControlManager;
import com.click.app.service.EmployeeManager;
import com.click.app.service.PaymentsSalaryManager;
import com.click.app.service.SalaryManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author ariel
 */
public class PaymentsSalaryAction extends BaseAction {

    private PaymentsSalaryManager paymentsSalaryManager;
    private AsistanceControlManager asistanceControlManager;
    private EmployeeManager employeeManager;
    private SalaryManager salaryManager;
    private List<PaymentsSalary> paymentsSalaries;
    private PaymentsSalary paymentsSalary;
    private Long id;
    private Employee employee;
    private Long idEmployee;
    private boolean editable;
    private boolean canAdd;
    private Salary salary;
    private List<LabelValue> typePay;

    public void setId(Long id) {
        this.id = id;
    }

    public void setIdEmployee(Long idEmployee) {
        this.idEmployee = idEmployee;
    }

    public PaymentsSalary getPaymentsSalary() {
        return paymentsSalary;
    }

    public void setPaymentsSalary(PaymentsSalary paymentsSalary) {
        this.paymentsSalary = paymentsSalary;
    }

    public Employee getEmployee() {
        return employee;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public boolean isCanAdd() {
        return canAdd;
    }

    public Salary getSalary() {
        return salary;
    }

    public List<LabelValue> getTypePay() {
        return typePay;
    }

    public String generateSalaryList() {
        System.out.println("generating cutProcessList...");
        if (editable) {
            employee = (Employee) getSession().getAttribute("employee");
            if (id == null) {
                salary = salaryManager.findByEmployeePay(employee);
            } else {
                PaymentsSalary pS = paymentsSalaryManager.get(id);
                salary = pS.getSalary();
                if (salary.getPaged()) {
                    salary.setPaged(false);
                } else {
                    salary.setHalfPay(false);
                }
            }
        } else {
            PaymentsSalary pS = paymentsSalaryManager.get(id);
            salary = pS.getSalary();
        }

        return SUCCESS;
    }

    public String delete() {
        System.out.println("deleting PaymentsSalary...");

        Salary s = salaryManager.get(paymentsSalary.getSalary().getId());
        paymentsSalary = paymentsSalaryManager.get(paymentsSalary.getId());
        if (paymentsSalary.getTypePay().equals("half")) {
            if (s.getPaged()) {
                s.setPaged(false);
            } else {
                s.setHalfPay(false);
            }
        } else {
            s.setPaged(false);
        }
        salaryManager.save(s);

        paymentsSalaryManager.remove(paymentsSalary.getId());
        saveMessage(getText("paymentsSalary.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            paymentsSalary = paymentsSalaryManager.get(id);
            employee = (Employee) getSession().getAttribute("employee");
            if (paymentsSalaryManager.lastPaymentSalary(employee).getId().equals(paymentsSalary.getId())) {
                setEditable(true);
            }
            typePay = new ArrayList<LabelValue>();
            typePay.add(new LabelValue(getText("paymentsSalary.half"), "half"));
            Salary s = paymentsSalary.getSalary();
            boolean paged = s.getPaged();
            boolean halfPay = s.getHalfPay();
            if (paged) {
                paged = false;
            } else {
                halfPay = false;
            }
            if (!halfPay) {
                Date currentDate = new Date(System.currentTimeMillis());
                List<AsistanceControl> aCL = asistanceControlManager.findByEmployeePeriod(employee, s.getInitDate(), s.getEndDate());
                if (aCL != null && aCL.size() > 0) {
                    s.getEndDate().setTime(s.getEndDate().getTime() + (86400000 * aCL.size()));
                    if (s.getEndDate().before(currentDate)) {
                        typePay.add(new LabelValue(getText("paymentsSalary.complete"), "complete"));
                    }
                } else {
                    if (s.getEndDate().before(currentDate)) {
                        typePay.add(new LabelValue(getText("paymentsSalary.complete"), "complete"));
                    }
                }
            }
        } else {
            paymentsSalary = new PaymentsSalary();
            employee = (Employee) getSession().getAttribute("employee");
            setEditable(true);
            typePay = new ArrayList<LabelValue>();
            typePay.add(new LabelValue(getText("paymentsSalary.half"), "half"));
            Salary s = salaryManager.findByEmployeePay(employee);
            if (!s.getHalfPay()) {
                Date currentDate = new Date(System.currentTimeMillis());
                List<AsistanceControl> aCL = asistanceControlManager.findByEmployeePeriod(employee, s.getInitDate(), s.getEndDate());
                if (aCL != null && aCL.size() > 0) {
                    s.getEndDate().setTime(s.getEndDate().getTime() + (86400000 * aCL.size()));
                    if (s.getEndDate().before(currentDate)) {
                        typePay.add(new LabelValue(getText("paymentsSalary.complete"), "complete"));
                    }
                } else {
                    if (s.getEndDate().before(currentDate)) {
                        typePay.add(new LabelValue(getText("paymentsSalary.complete"), "complete"));
                    }
                }
            }
        }

        paymentsSalary.setTypePay("half");
        paymentsSalary.setDatePay(new Date(System.currentTimeMillis()));

        return SUCCESS;
    }

    public String save() throws Exception {
        System.out.println("saving PaymentsSalary...");

        boolean isNew = (paymentsSalary.getId() == null);

        employee = (Employee) getSession().getAttribute("employee");

        if (isNew) {
            Salary s = salaryManager.findByEmployeePay(employee);
            if (s.getHalfPay()) {
                s.setPaged(true);
                paymentsSalary.setAmount(s.getAmount() / 2);
            } else {
                if (paymentsSalary.getTypePay().equals("half")) {
                    s.setHalfPay(true);
                    paymentsSalary.setAmount(s.getAmount() / 2);
                } else {
                    s.setPaged(true);
                    paymentsSalary.setAmount(s.getAmount());
                }
            }
            salaryManager.save(s);
            paymentsSalary.setSalary(s);
        } else {
            Salary s = salaryManager.get(paymentsSalary.getSalary().getId());
            if (s.getPaged()) {
                s.setPaged(false);
            } else {
                s.setHalfPay(false);
            }
            if (s.getHalfPay()) {
                s.setPaged(true);
                paymentsSalary.setAmount(s.getAmount() / 2);
            } else {
                if (paymentsSalary.getTypePay().equals("half")) {
                    s.setHalfPay(true);
                    paymentsSalary.setAmount(s.getAmount() / 2);
                } else {
                    s.setPaged(true);
                    paymentsSalary.setAmount(s.getAmount());
                }
            }
            salaryManager.save(s);
            paymentsSalary.setSalary(s);
        }

        paymentsSalary = paymentsSalaryManager.save(paymentsSalary);

        String key = (isNew) ? "paymentsSalary.added" : "paymentsSalary.updated";
        saveMessage(getText(key));

        return SUCCESS;
    }

    public String back() {
        getSession().removeAttribute("employee");
        return SUCCESS;
    }

    public void setPaymentsSalaryManager(PaymentsSalaryManager paymentsSalaryManager) {
        this.paymentsSalaryManager = paymentsSalaryManager;
    }

    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    public void setAsistanceControlManager(AsistanceControlManager asistanceControlManager) {
        this.asistanceControlManager = asistanceControlManager;
    }

    public void setSalaryManager(SalaryManager salaryManager) {
        this.salaryManager = salaryManager;
    }

    public List<PaymentsSalary> getPaymentsSalaries() {
        return paymentsSalaries;
    }

    public String list() {
        if (idEmployee != null) {
            employee = employeeManager.get(idEmployee);
            getSession().setAttribute("employee", getEmployee());
        } else {
            employee = (Employee) getSession().getAttribute("employee");
        }

        if (employee != null) {
            paymentsSalaries = paymentsSalaryManager.findByEmployee(employee);
            Salary s = salaryManager.findByEmployeePay(employee);
            if (s != null) {
                Date currentDate = new Date(System.currentTimeMillis());
                if (s.getHalfPay()) {
                    List<AsistanceControl> aCL = asistanceControlManager.findByEmployeePeriod(employee, s.getInitDate(), s.getEndDate());
                    if (aCL != null && aCL.size() > 0) {
                        s.getEndDate().setTime(s.getEndDate().getTime() + (86400000 * aCL.size()));
                        if (s.getEndDate().before(currentDate)) {
                            canAdd = true;
                        }
                    } else {
                        if (s.getEndDate().before(currentDate)) {
                            canAdd = true;
                        }
                    }
                } else {
                    Date middleDate = new Date(s.getInitDate().getTime() + 1209600000);
                    List<AsistanceControl> aCL = asistanceControlManager.findByEmployeePeriod(employee, s.getInitDate(), middleDate);
                    if (aCL != null && aCL.size() > 0) {
                        middleDate.setTime(middleDate.getTime() + (86400000 * aCL.size()));
                        if (middleDate.before(currentDate)) {
                            canAdd = true;
                        }
                    } else {
                        if (middleDate.before(currentDate)) {
                            canAdd = true;
                        }
                    }
                }
            }
            return SUCCESS;
        } else {
            System.out.println("employee is null: idEmployee = " + idEmployee);
            return ERROR;
        }
    }

    public String help() {
        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }
}
